#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : SettlementService.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-09-06 09:47
@Desc    :
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from report_system.other.settlement.models.db_model import *
from report_system.other.settlement.models.house import House
from report_system.other.settlement.models.investor import Investor


class InvestorService(object):
    def __init__(self, invest_id=None):
        self.invest_id = invest_id
        self.investor = Investor()
        self.relation_houses = None

    def init_investor(self):
        """
        初始化投资人所有信息
        :return:
        """
        self.investor.username = UsersModel(user_id=self.invest_id).get_by_user_id().username
        self.investor.invest_id = self.invest_id
        self.investor.houses = list()
        self.process_houses()

    def process_relation_houses(self):
        """
        获取 invest_relation_record 中的数据 本投资人所有房源关联关系
        :return:
        """
        self.relation_houses = InvestRelationRecordModel(invest_id=self.invest_id).get_by_invest_id()

    def process_compact_house(self, house_id):
        """
        获取表invest_compact_house 信息 租房合同开始、结束时间
        :param house_id:
        :return:
        """
        return InvestCompactHouseModel(invest_id=self.invest_id, house_id=house_id).get_by_invest_id_house_id()

    def process_houses(self):
        """
        循环处理本投资人每个房源数据
        :return:
        """
        self.process_relation_houses()
        for relation_house in self.relation_houses:
            self.process_house(relation_house)

    def process_house(self, relation_house):
        house = House()
        house.house_id = relation_house.house_id
        # 关联投资人的时间
        house.date_begin = relation_house.date_begin
        # 关联合同解除的时间
        house.date_relieve = relation_house.date_relieve

        compact_house = self.process_compact_house(house.house_id)

        # 租赁开始日期
        house.lease_date_begin = compact_house.lease_date_begin
        # 租赁结束日期
        house.lease_date_end = compact_house.lease_date_end

        print(house)
        self.investor.houses.append(house)

    def process(self):
        self.init_investor()


class HouseService(object):
    pass
